package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断当前加入菜品是否已经存在了
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart>  list= shoppingCartMapper.list(shoppingCart);
        //如果存在，数量加一

        if (list.size()>0 && list !=null) {
            ShoppingCart shoppingCart1 = list.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber()+1);
            shoppingCart1.setAmount(shoppingCart1.getAmount());
            shoppingCartMapper.updateNumber(shoppingCart1);
        }
        else {
            //如果不存在添加数据
            Long dishId = shoppingCart.getDishId();
            if(dishId!=null) {
                //如果菜品不为空
                Dish byId = dishMapper.getById(dishId);
                shoppingCart.setAmount(byId.getPrice());
                shoppingCart.setImage(byId.getImage());
                shoppingCart.setName(byId.getName());
            }else {
                //是套餐
                SetmealVO byId = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setAmount(byId.getPrice());
                shoppingCart.setImage(byId.getImage());
                shoppingCart.setName(byId.getName());
            }

            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查询购物车
     */
    public List<ShoppingCart> getShoppingCarts() {
        //当前用户id
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * 清空购物车
     */
    public void cleanShoppingCarts() {
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper.cleanByUserId(currentId);
    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart>  list= shoppingCartMapper.list(shoppingCart);
        //数据库中的数据1
        ShoppingCart shoppingCart1 = list.get(0);

        Long dishId = shoppingCart.getDishId();
        shoppingCart1.setNumber(shoppingCart1.getNumber()-1);

        //数量为0删除
       if (shoppingCart1.getNumber()==0){
           shoppingCartMapper.DeleteById(shoppingCart1.getId());
           return;
       }else {
           //数量不为0更新
           shoppingCartMapper.updateNumber(shoppingCart1);

       }
    }
}
